package ltd.latewhale.whalenest.controller;

import ltd.latewhale.whalenest.DTO.commentsDTO;
import ltd.latewhale.whalenest.model.ProjectInformationDO;
import ltd.latewhale.whalenest.service.ProjectInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员信息
 */
@RestController
@CrossOrigin //跨域
@RequestMapping("/ProjectInformation")
@Validated
public class ProjectInformationController {

    @Autowired
    private ProjectInformationService ProjectInformationService;

    //按id查询单个数据
    @GetMapping("/{id}")
    public ProjectInformationDO getProjectInformation(
            @PathVariable String id){
        ProjectInformationDO ProjectInformations = ProjectInformationService.getById(id);

        System.out.println("查询id为 "+id+" 的数据");

        if(ProjectInformations == null){
            return null;
        }
        return  ProjectInformations;
    };

    //查询全部数据
    @GetMapping("")
    public List<ProjectInformationDO> getProjectInformations(){
        List<ProjectInformationDO> ProjectInformations = ProjectInformationService.getProjectInformation();

        System.out.println("查询全部数据");

        if(ProjectInformations == null){
            return null;
        }
        return ProjectInformations;
    }

    //按id删除单个数据
    @DeleteMapping("/{id}")
    public Integer deleteProjectInformations(
            @PathVariable Integer id
    ){
        System.out.println(id);

        Integer ProjectInformations = ProjectInformationService.deleteId(id);

        System.out.println(id);

        if(ProjectInformations == 0){
            return null;
        }
        return 200;
    };

    //新增数据
    @PostMapping("")
    public Integer postProjectInformation(
            @RequestBody @Validated ProjectInformationDO dto
    ){
        System.out.println(dto);

        Integer ProjectInformation = ProjectInformationService.postProjectInformation(dto);

        System.out.println(dto);

        if(ProjectInformation == null){
            return null;
        }
        return 200;
    }

    //修改数据
    @PutMapping("")
    public Integer putProjectInformation(
            @RequestBody @Validated ProjectInformationDO dto
    ){
        Integer ProjectInformation = ProjectInformationService.putProjectInformation(dto);

        System.out.println("修改数据"+dto);

        if(ProjectInformation == null){
            return null;
        }
        return 200;
    }

    //评论表查询
    @GetMapping("/comments/{id}")
    public List<commentsDTO> commentsData(
            @PathVariable Integer id
    ){
        List<commentsDTO> comments = ProjectInformationService.commentsData(id);
        return comments;
    }

    //psupport ++
    @PutMapping("/psupportAdd/{id}")
    public void psupportAdd(
            @PathVariable Integer id
    ){
        ProjectInformationService.psupportAdd(id);
    }
}
